package com.atguigu.gulimail.product.service.impl;

import com.atguigu.gulimail.product.dao.AttrAttrgroupRelationDao;
import com.atguigu.gulimail.product.dao.AttrDao;
import com.atguigu.gulimail.product.entity.AttrAttrgroupRelationEntity;
import com.atguigu.gulimail.product.entity.AttrEntity;
import com.atguigu.gulimail.product.service.AttrService;
import com.atguigu.gulimail.product.vo.AttrGroupWithAttrVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimail.product.dao.AttrGroupDao;
import com.atguigu.gulimail.product.entity.AttrGroupEntity;
import com.atguigu.gulimail.product.service.AttrGroupService;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Resource
    AttrGroupDao attrGroupDao;

    @Resource
    AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 属性分组 -- 查询对应三级分类菜单的属性列表
     * 三级分类id传入0 -- 查全部
     *
     * @param params    -- 包含分页参数page、limit 和 模糊匹配参数key
     * @param catelogId
     * @return
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        if (catelogId == 0) {
            QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<>();
            return new PageUtils(getPage(params, wrapper));
        } else {
            QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<AttrGroupEntity>();
            wrapper.eq("catelog_id", catelogId);
            return new PageUtils(getPage(params, wrapper));
        }
    }

    /**
     * 获取当前分类下所有的分组以及其属性
     * @param catelogId
     * @return
     */
    @Override
    public List<AttrGroupWithAttrVo> getAttrGroupWithAttrByCatelogId(Long catelogId) {
        List<AttrGroupEntity> attrGroupEntities = attrGroupDao.selectList(new QueryWrapper<AttrGroupEntity>().
                eq("catelog_id", catelogId));
        return attrGroupEntities.stream().map(item -> {
            AttrGroupWithAttrVo vo = new AttrGroupWithAttrVo();
            BeanUtils.copyProperties(item, vo);
            List<AttrEntity> attrEntities = attrService.getRelationAttr(vo.getAttrGroupId());
            vo.setAttrs(attrEntities);
            return vo;
        }).filter(i -> i.getAttrs() != null).collect(Collectors.toList());
    }

    /**
     * select * from pms_attr_group where catelog_id = ? and
     * (attr_group_id = key or attr_group_name like %key%)
     */
    private IPage<AttrGroupEntity> getPage(Map<String, Object> params, QueryWrapper<AttrGroupEntity> wrapper) {
        String key = (String) params.get("key");
        if (!StringUtils.isEmpty(key)) {
            wrapper.and((obj) -> {
                obj.eq("attr_group_id", key).or()
                        .like("attr_group_name", key);
            });
        }
        return this.page(
                new Query<AttrGroupEntity>().getPage(params), wrapper
        );
    }

}